<?php
namespace App\Core\Utils\TwHelper;

class Environment
{

    const ENV_RELEASE = 'master';

    const ENV_DEV = 'dev';

    const ENV_LOCAL = 'local';

    /**
     * 当前运行环境是否是本地开发环境
     *
     * @return bool
     */
    public static function isLocal()
    {
        return (env('RUN_ENV') == self::ENV_LOCAL);
    }

    /**
     * 当前运行环境是否是开发测试环境
     *
     * @return bool
     */
    public static function isDevelopment()
    {
        return (env('RUN_ENV') == self::ENV_DEV);
    }

    /**
     * 当前运行环境是否是正式生产环境
     *
     * @return bool
     */
    public static function isProduction()
    {
        return (env('RUN_ENV') == self::ENV_RELEASE);
    }

    /**
     * 当前运行环境是否是通威内网
     *
     * @return bool
     */
    public static function isIntranet()
    {
        $allowIps = [
            [
                '127',
                '0',
                '0'
            ], // localhost
            [
                '125',
                '71',
                '200'
            ], // 电信
            [
                '182',
                '148',
                '106'
            ], // 电信
            [
                '110',
                '188',
                '66'
            ], // 电信
            [
                '101',
                '204',
                '230'
            ], // 网通
            [
                '183',
                '221',
                '117'
            ]
        ]; // 移动

        $ip = self::getRealIP();
        
        $ips = explode('.', $ip);
        foreach ($allowIps as $allowIP) {
            $result = (in_array($ips[0], $allowIP) && in_array($ips[1], $allowIP) && in_array($ips[2], $allowIP));
            if ($result) {
                break;
            }
        }
        
        return $result;
    }

    /**
     * 获取客户端真实IP
     *
     * @return array|false|string
     */
    public static function getRealIP()
    {
        if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
            $ip = getenv("HTTP_CLIENT_IP");
        } else {
            if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
                $ip = getenv("HTTP_X_FORWARDED_FOR");
            } else {
                if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
                    $ip = getenv("REMOTE_ADDR");
                } else {
                    if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
                        $ip = $_SERVER['REMOTE_ADDR'];
                    } else {
                        $ip = "unknown";
                    }
                }
            }
        }
        
        return ($ip);
    }
    
    /**
     * 是否 ie6~ie9 访问
     *
     * @return bool
     */
    public static function isOldIe() 
    {
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 9.0')) {
            return true;
        } else if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 8.0')) {
            return true;
        } else if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 7.0')) {
            return true;
        } else if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.0')) {
            return true;
        }
        return false;
    }  
    
    /**
     * 是否移动端访问
     * @return bool
     */
    public static function isMobile() {
        // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
        if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
            return true;
        }
        // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
        if (isset($_SERVER['HTTP_VIA'])) { // 找不到为flase,否则为true
            return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
        }
        // 脑残法，判断手机发送的客户端标志,兼容性有待提高
        if (isset($_SERVER['HTTP_USER_AGENT'])) {
            $clientkeywords = array(
                'nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic',
                'alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb',
                'windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile'
            );
            // 从HTTP_USER_AGENT中查找手机浏览器的关键字
            if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
                return true;
            }
        }
        // 协议法，因为有可能不准确，放到最后判断
        if (isset($_SERVER['HTTP_ACCEPT'])) {
            // 如果只支持wml并且不支持html那一定是移动设备
            // 如果支持wml和html但是wml在html之前则是移动设备
            if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
                return true;
            }
        }
        return false;
    }
    
    /**
     * 判断是否是微信环境
     * 
     * @return boolean
     */
    public static function isWeixin() {
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
            return true;
        }
        return false;
    }
    
    /**
     * 判断是否是ExMobi环境
     * 
     * @return boolean
     */
    public static function isExMobi() {
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'ExMobi') !== false) {
            return true;
        }
        return false;
    } 
    
    /**
     * 判断当前系统环境是否为window
     * 
     * @return boolean
     */
    public static function isWindow() {
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
            return true;
        }
        return false;
    }

    public static function makeDate($time, $mark = '.', $status = true)
    {
        $timeDiff = time() - $time;
        if ($status && $timeDiff < 60) {
            $timeString = '刚刚';
        } elseif ($status && $timeDiff < 3600) {
            $timeString = floor($timeDiff / 60) . '分钟前';
        } elseif ($status && $timeDiff < 86400) {
            $timeString = floor($timeDiff / 3600) . '小时前';
        } elseif ($status && $timeDiff < 2592000) {
            $timeString = floor($timeDiff / 86400) . '天前';
        } else {
            $timeString = date('Y' . $mark . 'm' . $mark . 'd', $time);
        }
        return $timeString;
    }

}